import {
  post,
  ajaxSetup,
} from "jquery"
import * as interfaces from './Interfaces.json'

function csrfSafeMethod(method) {
  // these HTTP methods do not require CSRF protection
  return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

// 添加csrf 验证
// 添加日志
ajaxSetup({
  beforeSend: function (xhr, settings) {
    if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
      let arr = document.cookie.split(';')
      for (let v in arr) {
        let to = arr[v].split('=');
        if (to[0].replace(/\s+/g, "") === "csrfToken") {
          xhr.setRequestHeader('x-csrf-token', to[1].replace(/\s+/g, ""));
        }
      }
    }
  },
});
export const asynclogin = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.login, data).then(response => {
      if (response.error == 0) {
        window.userid = response.data.userid
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncloginphonenum = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.loginphonenum, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function () {

      reject('服务器开小差了')
    })
  })
}

export const session = function () {
  return new Promise((resolve, reject) => {
    post(interfaces.online).then(response => {
      if (response.error == 0) {
        window.userid = response.data.userid
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// 用户注册
export const asyncregist = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.regist, data).then(response => {
      if (response.error == 0) {

        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// 获取子用户
export const asyncgetChildren = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getChildren, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// 获取课程包
export const asyncgetInvite = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getInvite, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// 获取yzm
export const asyncfind_getCode = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.find_getCode, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// 检查yzm
export const asyncfind_checkCode = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.find_checkCode, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}
// 修改密码
export const asyncfind_findPassword = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.find_findPassword, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}
// 注册获取yzm
export const asyncregist_getCode = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.regist_getCode, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// wx获取验证码
export const asyncgetLoginQRCode = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getLoginQRCode, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

// wx绑定
export const asyncgetBindQRCode = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getBindQRCode, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}


// WXAuthorize
export const asyncWXAuthorize = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.WXAuthorize).then(response => {
      if (response.error == 0) {
        resolve(response.data, data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const wechatsignature = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.wechatsignature, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncforum = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.forum, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncgettopicbyid = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.gettopicbyid, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncgettopic = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.gettopic, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncgetreply = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getreply, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asynctopiccount = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.topiccount, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}
export const asyncselectanswer = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.selectanswer, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asynccreatanswer = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.creatanswer, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncgetlibrayvideo = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getlibrayvideo, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncgetlibrayvideolist = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getlibrayvideolist, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncchangestudentopenid = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.changestudentopenid, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

export const asyncgetstudentyzm = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getstudentyzm, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

/**
 * 通过id获取编程机器人
 * @param {*} data 
 */
export const asyncgetcodingrobot = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getcodingrobot, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

/**
 * 获取编程机器人视频列表
 * @param {*} data 
 */
export const asyncgetcodingrobotlist = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getcodingrobotlist, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}


/**
 * 通过id获取编程机器人
 * @param {*} data 
 */
export const asyncgetmicro = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getmicro, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}

/**
 * 获取编程机器人视频列表
 * @param {*} data 
 */
export const asyncgetmicrolist = function (data) {
  return new Promise((resolve, reject) => {
    post(interfaces.getmicrolist, data).then(response => {
      if (response.error == 0) {
        resolve(response.data)
      } else {
        reject(response.msg)
      }
    }).catch(function (error) {
      console.log(error);
      reject('服务器开小差了')
    })
  })
}
